package com.xxx.demo.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxx.demo.entity.TUsers;
import com.xxx.demo.entity.TUsersExample;
import com.xxx.demo.mapper.TUsersMapper;
import com.xxx.demo.tiaoJian.UserTiaoJian;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserServiceImpl {
    @Autowired(required = false)
    private TUsersMapper userDao;

    //public TUsers doLogin(String userName, String password) {
    //   TUsersExample condition=new TUsersExample();
    //   condition.createCriteria().andUserNameEqualTo(userName).andPasswordEqualTo(password);
    //   List<TUsers> users= userDao.selectByExample(condition);
    //   if (users.size()==0){
    //       return null;
    //   }else
    //       return users.get(0);
    //}
    //
    //
    //新增用户
    @Transactional
    public void saveUser(TUsers users){
        userDao.insert(users);
    }
    //
    ////查询用户
    //@Transactional(readOnly = true)
    //public List<TUsers> findAllUser(){
    //   return (List<TUsers>) userDao.selectByExample(new TUsersExample());
    //}

    @Transactional
    //修改用户
    public void updateUser(TUsers users){
        userDao.updateById(users);
    }

    /**
     * 通过id查找用户
     * @param id
     * @return
     */
    @Transactional(readOnly = true)
    public TUsers queryUserById(Long id){
        return userDao.selectById(id);
    }
    //
    ///**
    // * 通过条件查询用户
    // * @param userTiaoJian
    // * @return
    // */
    //@Transactional(readOnly = true)
    //public List<TUsers> queryUserByTiaoJian(UserTiaoJian userTiaoJian){
    //    return userDao.queryUserByTiaoJian(userTiaoJian);
    //}
    //
    ///**
    // * 根据条件查询用户总数
    // * @param userTiaoJian
    // * @return
    // */
    //@Transactional(readOnly = true)
    //public Integer queryCountUser(UserTiaoJian userTiaoJian){
    //    return userDao.queryCountUser(userTiaoJian);
    //}
    //
    /**
     * 通过用户id删除用户(假删除)
     * @param id
     * @return
     */
    @Transactional
    public Integer deleteUserById(Long id) throws Exception {
        TUsers tUsers = userDao.selectById(id);
        if (ObjectUtil.isEmpty(tUsers)){
            throw new Exception("数据不存在！");
        }
        return userDao.deleteByPrimaryKey(id);
    }


    //=======================================以下代码是改造代码======================================

    /**
     * 通过账号密码查询管理员
     * @param userName
     * @param password
     * @return
     */
    @Transactional(readOnly = true)
    public TUsers selectAdmin(String userName,String password){
        QueryWrapper<TUsers> queryWrapper=new QueryWrapper<TUsers>();
        queryWrapper.eq("user_name",userName).eq("password",password).eq("delete_flag",0);
        return userDao.selectOne(queryWrapper);
    }

    /**
     * 通过分页查询管理员
     * @param pageNo
     * @param pageSize
     * @param users
     * @return
     */
    @Transactional(readOnly = true)
    public IPage<TUsers> selectAdminPage(Integer pageNo, Integer pageSize, TUsers users){
        Page<TUsers> page=new Page<>(pageNo,pageSize);
        return userDao.selectUser(page,users);
    }
}
